package package004.section002

import fundation.gsonPrintln
import org.junit.Test

/**
 * @author futao
 * Created on 2017/11/15 - 18:53.
 */
class User(val name: String) {
    var address: String = "unspecified"
        set(value) {
            println("""
        Address was changed for $name:
        "$field" -> "$value".""".trimIndent())
            field = value
        }
    var province: String = "江西省"
        set(value) {
            println("$field  ============ $value")
        }
}

fun main(args: Array<String>) {
//    val user = User("Alice")
//    user.address=""
//    println("=======================-------------")
//
//    user.province="上海市"
//    user.province="武汉市"
//    user.province="南昌市"
//    user.province="北京"
//    user.province="杭州"
//    user.province="无锡"
//    println("===="+user.province)
//    val countLength = CountLength()
//    gsonPrintln(countLength.counter)
//    gsonPrintln(countLength.counter)
//    gsonPrintln(countLength.addWord("pppppppppppppp"))
//    gsonPrintln(countLength.counter)
//    gsonPrintln(countLength.addWord("pppppppppppppp"))
//    gsonPrintln(countLength.counter)
//    gsonPrintln(countLength.addWord("pppppppppppppp"))
//    gsonPrintln(countLength.counter)

    val countLength = CountLength().apply {
        argument = "1000"
    }
    val countLength1 = CountLength().apply {
        argument = "1000"
    }
    /**
     * 重写了equals方法
     */
    gsonPrintln(countLength==countLength1)
    /**
     * === 比较对象引用
     */
    gsonPrintln(countLength===countLength1)

}

class CountLength {
    var counter: Int = 0
        private set
    lateinit var argument: String

    constructor()

    constructor(counter: Int, argument: String) {
        this.counter = counter
        this.argument = argument
    }

    fun addWord(word: String) {
        this.counter += word.length
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as CountLength

        if (counter != other.counter) return false
        if (argument != other.argument) return false

        return true
    }

    override fun hashCode(): Int {
        var result = counter
        result = 31 * result + argument.hashCode()
        return result
    }


}